<?php if (! defined('APPPATH')) die('No direct script access allowed');
/**
 * RENARTI IK
 * An open source application development framework for PHP 5.2.0 or newer
 *
 * @package		RtiFramework
 * @author		Nauris Dambis <nauris.dambis@renarti.com>
 * @copyright	Copyright (c) 2012, RENARTI IK.
 * @license		http://renarti.com/#license
 * @link		http://renarti.com
 * @since		Version 1.0
 */

class RtiInput
{
	private $_remote_ip = FALSE;
	
	private $_request;
	
	private $_errors;
	
	private $_success = ''; 
	
	public function __construct()
	{
		$this->_errors  = array();
		$this->_request = $_REQUEST;
		$this->_read_remote_ip();
	}
	
	public function hasData()
	{
		return (! empty($this->_request));	
	}
	
	public function remoteIp()
	{
		return $this->_remote_ip;
	}
	
	public function errors()
	{
		return $this->_errors;
	}
	
	public function hasError()
	{
		return (! empty($this->_errors));
	}
	
	public function successMessage()
	{
		return $this->_success;
	}
	
	public function validate($rules, $translations)
	{
		$this->_errors = array();
		
		$fields = array_keys($rules);
		
		foreach ($this->_request as $field => $value)
		{
			if (in_array($field, $fields))
			{
				$patterns = $rules[$field];
				
				foreach ($patterns as $_rule)
				{
					$pattern = $_rule[0];
					$error   = $_rule[1];
					
					$value = trim($value);
					if (! preg_match($pattern, $value))
					{
						$this->_errors[] = $translations[$error];
						break;
					}					
				}				
			}
		}
		
		return empty($this->_errors);
	}
	
	public function addError($text)
	{
		$this->_errors[] = $text;
	}
	
	public function addSuccess($text)
	{
		$this->_success = $text;
	}  
	
	public function readFlash($key = 'form')
	{
		$this->_request = array();
		$this->_errors  = array();
		$this->_success = '';
		
		$array = Rti::session()->readFlash($key);
		if (! empty($array))
		{
			$this->_request = $array['request'];
			$this->_errors  = $array['errors'];			
			$this->_success = $array['success'];
		}
	}

	public function writeFlash($key = 'form')
	{
		$array['request'] = $this->_request;
		$array['errors']  = $this->_errors;		
		$array['success'] = $this->_success;
		
		Rti::session()->writeFlash($key, $array);		
	}
	
	public function set($field, $value = '')
	{
		if (is_array($field) || is_object($field))
		{
			foreach ($field as $k => $v)
			{
				$this->set($k, $v);
			}
		}
		else
		{
			$this->_request[$field] = $value;
		}
	}
	
	public function get($field = null, $html = FALSE)
	{
		if (empty($field))
		{
			$result = $this->_request;
			
			if ($html === TRUE)
			{
				foreach ($result as $k=>$v)
				{
					$result[$k] = htmlspecialchars($v);
				}
			}
			
			return $result;			
		}
		else if (isset($this->_request[$field]))
		{
			if ($html === FALSE)
			{
				return $this->_request[$field];
			}
			else 
			{
				return htmlspecialchars($this->_request[$field]);
			}
		}
		
		return '';
	}
	
	private function _server($key)
	{
		if (isset($_SERVER[$key]))
		{
			return TRUE;
		}
		return FALSE;
	}
	
	private function _read_remote_ip()
	{
		if ($this->_server('REMOTE_ADDR') AND $this->_server('HTTP_CLIENT_IP'))
		{
			$this->_remote_ip = $_SERVER['HTTP_CLIENT_IP'];
		}
		elseif ($this->_server('REMOTE_ADDR'))
		{
			$this->_remote_ip = $_SERVER['REMOTE_ADDR'];
		}
		elseif ($this->_server('HTTP_CLIENT_IP'))
		{
			$this->_remote_ip = $_SERVER['HTTP_CLIENT_IP'];
		}
		elseif ($this->_server('HTTP_X_FORWARDED_FOR'))
		{
			$this->_remote_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
		}

		if ($this->_remote_ip === FALSE)
		{
			$this->_remote_ip = '0.0.0.0';
		}
	}
}


/* End of file RtiInput.php */
/* Location: ./system/core/RtiInput.php */
